import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { handleTree } from '@vben/utils';

import { getDeptList } from '#/api/system/dept';

import { $t } from '#/locales';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: $t('views.crm.config.businessStatus.statusGroupName'),
      component: 'Input',
      rules: 'required',
      componentProps: {
        placeholder: $t('views.crm.config.businessStatus.statusGroupNamePlaceholder'),
      },
    },
    {
      fieldName: 'deptIds',
      label: $t('views.crm.config.businessStatus.deptIds'),
      component: 'ApiTreeSelect',
      componentProps: {
        api: async () => {
          const data = await getDeptList();
          return handleTree(data);
        },
        multiple: true,
        fieldNames: { label: 'name', value: 'id', children: 'children' },
        placeholder: $t('views.crm.config.businessStatus.deptIdsPlaceholder'),
        treeDefaultExpandAll: true,
      },
      help: $t('views.crm.config.businessStatus.deptIdsHelpMessage'),
    },
    {
      fieldName: 'statuses',
      label: $t('views.crm.config.businessStatus.phasesSetting'),
      component: 'Input',
      rules: 'required',
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'name',
      title: $t('views.crm.config.businessStatus.statusGroupName'),
    },
    {
      field: 'deptNames',
      title: $t('views.crm.config.businessStatus.deptIds'),
      formatter: ({ cellValue }) =>
        cellValue?.length > 0 ? cellValue.join(' ') : $t('views.crm.config.businessStatus.wholeCompany'),
    },
    {
      field: 'creator',
      title: $t('views.common.creatorName'),
    },
    {
      field: 'createTime',
      title: $t('common.createTime'),
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      width: 160,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 商机状态阶段列表列配置 */
export function useFormColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'defaultStatus',
      title: $t('views.crm.config.businessStatus.phase'),
      minWidth: 100,
      slots: { default: 'defaultStatus' },
    },
    {
      field: 'name',
      title: $t('views.crm.config.businessStatus.phaseName'),
      minWidth: 100,
      slots: { default: 'name' },
    },
    {
      field: 'percent',
      title: $t('views.crm.config.businessStatus.percent'),
      minWidth: 100,
      slots: { default: 'percent' },
    },
    {
      title: $t('common.action'),
      width: 130,
      fixed: 'right',
      slots: { default: 'actions' },
    }
  ];
}
